<?php

namespace App\Jobs;

use App\Http\Controllers\EpsLngLatController;
use App\Jobs\Middleware\RateLimited4Gd;
use App\Models\EpsLngLat;
use Exception;
use Illuminate\Contracts\Queue\ShouldQueue;
use Illuminate\Foundation\Queue\Queueable;
use Illuminate\Queue\Middleware\WithoutOverlapping;
use Illuminate\Support\Facades\Log;

class GdGeoJob implements ShouldQueue
{
    use Queueable;

    /**
     * 任务可尝试的次数
     *
     * @var int
     */
    public $tries = 1;

    /**
     * ins
     * @var EpsLngLat|null
     * create_at: 2025-09-09 09:12:04
     * update_at: 2025-09-09 09:12:04
     */
    public $epLngLatInsId = null;

    /**
     * Create a new job instance.
     * @param $id
     */
    public function __construct(public $id)
    {
        $this->onQueue(EpsLngLatController::GD_ON_QUEUE);
        $this->epLngLatInsId = $id;
    }

    /**
     * 中间件
     * @return array
     * create_at: 2025-01-11 09:49:56
     * update_at: 2025-01-11 09:49:56
     */
    public function middleware(): array
    {
        return [new RateLimited4Gd, (new WithoutOverlapping($this->epLngLatInsId))->releaseAfter(1)];
    }

    /**
     * Execute the job.
     */
    public function handle(): void
    {
        try {
//            Log::info("ID 【{$this->epLngLatInsId}】开始执行");
            app(EpsLngLatController::class)->handleGdJob($this->epLngLatInsId);
            $this->delete();
        } catch (Exception $e) {
            Log::error("ID 【{$this->epLngLatInsId}】： " . $e->getMessage());
        }
    }
}
